package com.yatu.codegen.util;

import java.util.Date;

public final class ConvertUtil {
	private ConvertUtil() {
	}

	/**
	 * 把数据库中的字段转换为变量类型 如（USER_ID ----> userId）
	 */
	public static String convert(String name) {
		// 分隔符
		char separator = '_';
		// 转化为小写
		String variable = name.toLowerCase();

		if (variable.indexOf(separator) > -1) {
			char[] varArray = variable.toCharArray();
			for (int i = 0; i < varArray.length; i++) {
				if (varArray[i] == separator && i < varArray.length - 1) {
					varArray[i + 1] = Character.toUpperCase(varArray[i + 1]);
				}
			}
			variable = new String(varArray).replaceAll("_", "");
		}

		return variable;

	}

	// 首字母大写
	public static String format(String source) {
		if (source == null)
			return null;
		if (source.equals(""))
			return "";

		String a;
		a = source.substring(0, 1);
		a = a.toUpperCase();
		return a + source.substring(1);
	}

	public static String getTypeName(String type) {
		type = type.toUpperCase();
		String typeName = "String";
		if (type.startsWith("INT")) {
			typeName = Integer.class.getSimpleName();
		} else if (type.startsWith("NUMERIC") || type.startsWith("DECIMAL")) {
			typeName = Double.class.getSimpleName();
		} else if (type.startsWith("FLOAT")) {
			typeName = Float.class.getSimpleName();
		} else if (type.startsWith("TIMESTAMP") || type.startsWith("DATETIME") || type.startsWith("DATE")) {
			typeName = Date.class.getSimpleName();
		} else if (type.startsWith("BOOLEAN")) {
			typeName = Boolean.class.getSimpleName();
		}

		return typeName;
	}

}
